using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
namespace Pixysoft.Framework.Reports.Core
{
    class ReportColumn : IReportColumn
    {
        private Int32 columnIndex;
        private string identifier;
        private String headerName;
        private List<string> columnNames = new List<string>();
        private List<IReportColumn> columns = new List<IReportColumn>();
        private IReportColumn parent;
        private IReportColumnSetting setting;

        public ReportColumn(string headerName, int columnIndex, IReportColumnSetting setting)
        {
            this.headerName = headerName;
            this.columnIndex = columnIndex;
            this.setting = setting;
        }

        public void Initialize()
        {
            IReportColumn current = this;
            StringBuilder builder = new StringBuilder();
            while (current != null)
            {
                if (current.Setting.ExpressionType == ExpressionType.Group)
                {
                    if (!columnNames.Contains(current.Setting.ColumnName))
                    {
                        builder.Append(current.HeaderName);
                        columnNames.Add(current.Setting.ColumnName);
                    }
                }

                current = current.Parent;
            }
            this.identifier = builder.ToString();
        }

        public Int32 ColumnIndex
        {
            get
            {
                return columnIndex;
            }
        }

        public string Identifier
        {
            get { return identifier; }
        }

        public String HeaderName
        {
            get
            {
                return headerName;
            }
        }

        public string[] ColumnNames { get { return columnNames.ToArray(); } }

        public IReportColumnSetting Setting
        {
            get
            {
                return setting;
            }
        }

        public IReportColumn Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }

        public IReportColumnCollection Children
        {
            get
            {
                return new ReportColumnCollection(columns);
            }
        }

        public override string ToString()
        {
            return string.Format("column:{0},header:{1},index:{2}, id:{3}", setting.ColumnName, headerName, columnIndex, identifier);
        }
    }
}
